﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
using GMAO.CORE;
using Coolite.Ext.Web;

public partial class Menu : System.Web.UI.UserControl
{
    private log4net.ILog log;

    public log4net.ILog Log
    {
        get { return log; }
        set { log = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlNodeList nodeList = MenuClass.loadMenu("Menu","ID","PARENT_ID","SORT","LIBELE","DESCRIPTION","URL","SORT");
        /*
        tree.Listeners.Click.Handler = statusBar.ClientID + ".setStatus({text: 'Node Selected: <b>' + node.text + '</b>', clear: true});";
        tree.Listeners.ExpandNode.Handler = statusBar.ClientID + ".setStatus({text: 'Node Expanded: <b>' + node.text + '</b>', clear: true});";
        tree.Listeners.ExpandNode.Delay = 30;
        tree.Listeners.CollapseNode.Handler = statusBar.ClientID + ".setStatus({text: 'Node Collapsed: <b>' + node.text + '</b>', clear: true});";
        */
        AccordionMenu.HideCollapseTool = true;
        foreach (XmlNode node in nodeList)
        {
            XmlNode currentNode = node;
            Coolite.Ext.Web.TreePanel tree = new Coolite.Ext.Web.TreePanel();
            AccordionMenu.Items.Add(tree);
            Coolite.Ext.Web.TreeNode root = new Coolite.Ext.Web.TreeNode(currentNode.Attributes["LIBELE"].Value);
            root.Expanded = true;
            root.Qtip = currentNode.Attributes["DESCRIPTION"].Value;
            tree.Root.Add(root);
            tree.ID = "TreePanel" + currentNode.Attributes["ID"].Value;
            tree.Width = Unit.Pixel(300);
            tree.Height = Unit.Pixel(450);
            tree.Icon = Icon.UserSuit;
            tree.Title = currentNode.Attributes["LIBELE"].Value;
            tree.AutoScroll = true;

            Coolite.Ext.Web.Button btnExpand = new Coolite.Ext.Web.Button();
            btnExpand.Text = "Expand All";
            btnExpand.Listeners.Click.Handler = tree.ClientID + ".expandAll();";

            Coolite.Ext.Web.Button btnCollapse = new Coolite.Ext.Web.Button();
            btnCollapse.Text = "Collapse All";
            btnCollapse.Listeners.Click.Handler = tree.ClientID + ".collapseAll();";

            Toolbar toolBar = new Toolbar();
            toolBar.ID = "Toolbar" + currentNode.Attributes["ID"].Value; ;
            toolBar.Items.Add(btnExpand);
            toolBar.Items.Add(btnCollapse);
            tree.TopBar.Add(toolBar);

            StatusBar statusBar = new StatusBar();
            statusBar.AutoClear = 1000;
            tree.BottomBar.Add(statusBar);

            if (currentNode.HasChildNodes == true)
            {
                // Appel recursive pour les nœuds fils
                WalkTree(currentNode, ref root);
            }
        }
    }
    private void WalkTree(XmlNode node, ref Coolite.Ext.Web.TreeNode treeNode)
    {
        for (int j = 0; j < node.ChildNodes.Count; j++)
        {
            XmlNode newNode = node.ChildNodes[j];

            if (newNode.HasChildNodes == true)
            {
                Coolite.Ext.Web.TreeNode composerNode = new Coolite.Ext.Web.TreeNode(newNode.Attributes["LIBELE"].Value, Icon.Folder);
                composerNode.Qtip = newNode.Attributes["DESCRIPTION"].Value;
                treeNode.Nodes.Add(composerNode);
                WalkTree(newNode, ref composerNode);
            }
            else
            {
                Coolite.Ext.Web.TreeNode composerNode = new Coolite.Ext.Web.TreeNode(newNode.Attributes["LIBELE"].Value, Icon.UserGray);
                composerNode.Qtip = newNode.Attributes["DESCRIPTION"].Value;
                composerNode.Listeners.Click.Handler = "addTab(#{CenterPanel}, '" + newNode.Attributes["ID"].Value + "', '" + newNode.Attributes["URL"].Value + "','" + newNode.Attributes["LIBELE"].Value + "');";
                treeNode.Nodes.Add(composerNode);
            }
        }
    } 
}
